package com.xiaobai.common;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;

import java.io.Serializable;
import java.util.Calendar;

@JsonInclude(value= JsonInclude.Include.NON_NULL)
public class ServiceResponse<T> implements Serializable {

    private int status;
    private String msg;
    private T data;

    private ServiceResponse(int status) {
        this.status = status;
    }

    private ServiceResponse(int status, T data) {
        this.status = status;
        this.data = data;
    }
    //上下两个通过构造器的构造名来区分调用
    private ServiceResponse(int status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    private ServiceResponse(int status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    @JsonIgnore//使之不在json序列化当中
    public boolean isSuccess(){
        return this.status==ResponseCode.SUCCESS.getCode();//如果返回状态为0,则为true
    }

    public int getStatus() {
        return status;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }

    public static <T> ServiceResponse<T> createBySuccess(){
        //构造方法,仅一个状态码
        return new ServiceResponse<T>(ResponseCode.SUCCESS.getCode());
    }

    public static<T> ServiceResponse<T> createBySuccessMsg(String msg){
        //状态码+枚举提示
        return new ServiceResponse<T>(ResponseCode.SUCCESS.getCode(),msg);
    }

    public static<T> ServiceResponse<T> createBySuccess(T date){
        //状态码+数据
        return new ServiceResponse<T>(ResponseCode.SUCCESS.getCode(),date);
    }

    public static<T> ServiceResponse<T> createBySuccess(String msg,T date){
        //状态码+枚举提示+数据
        return new ServiceResponse<T>(ResponseCode.SUCCESS.getCode(),msg,date);
    }


    public static <T>ServiceResponse<T> createByError(){
        //状态码+枚举提示
        return new ServiceResponse<T>(ResponseCode.ERROR.getCode());
    }

    public static <T>ServiceResponse<T> createByErrorMsg(String msg){
        //状态码+自定义提示信息
        return new ServiceResponse<T>(ResponseCode.ERROR.getCode(),msg);
    }

    public static <T>ServiceResponse<T> createByErrorCodeMessage(int errorCode,String msg){
        //传入定义的枚举code
        //自定义状态码+自定义提示
        return new ServiceResponse<T>(errorCode,msg);
    }

    public static <T>ServiceResponse<T> fail(String msg) {
        return new ServiceResponse<T>(ResponseCode.ERROR.getCode(),msg);
    }

    public static <T>ServiceResponse<T> fail(T data) {
        return new ServiceResponse<T>(ResponseCode.ERROR.getCode(),data);
    }

    public static <T>ServiceResponse<T> fail(String msg, T data) {
        return new ServiceResponse<T>(ResponseCode.ERROR.getCode(),msg, data);
    }

    public static <T>ServiceResponse<T> failV2(String msg) {
        return new ServiceResponse<T>(ResponseCode.V2.getCode(),msg);
    }

    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        int i = calendar.get(Calendar.HOUR_OF_DAY);
        System.out.println(i);
    }

}
